package com.llsydn.beijingnews.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @author lilinshen
 * @title 缓存软件的一些参数和数据
 * @description 请填写相关描述
 * @date 2018/10/24 15:10
 */
public class CacheUtils {

    /**
     * 得到缓存值
     *
     * @param context
     * @param key
     * @return
     */
    public static boolean getBoolean(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("llsydn", Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, false);
    }

    /**
     * 保存缓存值
     *
     * @param context
     * @param key
     * @param value
     */
    public static void putBoolean(Context context, String key, boolean value) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("llsydn", Context.MODE_PRIVATE);
        sharedPreferences.edit().putBoolean(key, value).commit();
    }

    /**
     * 缓存文本
     *
     * @param context
     * @param key
     * @param value
     */
    public static void putString(Context context, String key, String value) {
        //判断sdcard是否挂载
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            try {
                String fileName = Md5Utls.encode(key);
                //得到sd卡下的files路径 /storage/emulated/0/Android/data/com.llsydn.beijingnews/files/text
                String filePath = context.getExternalFilesDir(null).getAbsolutePath() + "/text";
                File file = new File(filePath, fileName);
                File parentFile = file.getParentFile();
                if (!parentFile.exists()) {
                    //创建目录
                    parentFile.mkdirs();
                }
                if (!file.exists()) {
                    file.createNewFile();
                }
                //保存文本数据
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(value.getBytes());
                fileOutputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
                LogUtil.e("文本数据缓存失败");
            }
        } else {
            SharedPreferences sharedPreferences = context.getSharedPreferences("llsydn", Context.MODE_PRIVATE);
            sharedPreferences.edit().putString(key, value).commit();
        }
    }

    /**
     * 获取缓存文本
     *
     * @param context
     * @param key
     */
    public static String getString(Context context, String key) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            try {
                //xxxxxxxxx
                String fileName = Md5Utls.encode(key);
                //得到sd卡下的files路径 /storage/emulated/0/Android/data/com.llsydn.beijingnews/files/text
                String filePath = context.getExternalFilesDir(null).getAbsolutePath() + "/text";
                File file = new File(filePath, fileName);
                if (file.exists()) {
                    FileInputStream is = new FileInputStream(file);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int length = 0;
                    while ((length = is.read(buffer)) != -1) {
                        baos.write(buffer, 0, length);
                    }
                    String result = baos.toString();
                    baos.close();
                    is.close();
                    return result;
                }
            } catch (Exception e) {
                e.printStackTrace();
                LogUtil.e("文本数据获取失败");
            }
        } else {
            SharedPreferences sharedPreferences = context.getSharedPreferences("llsydn", Context.MODE_PRIVATE);
            return sharedPreferences.getString(key, "");
        }
        return "";
    }
}
